package cn.gailvlun.gll.presentation.login

import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityCaptchaBinding
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.NullSubscriber
import cn.gailvlun.gll.net.base.Profile
import cn.gailvlun.gll.net.login.*
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.util.*
import cn.gailvlun.gll.widget.edittext.FilterType



class CaptchaActivity : BaseActivity() {
    private lateinit var mBinding: ActivityCaptchaBinding
    private lateinit var mFromActivity: FromActivityEnum
    private lateinit var mPhoneNumber: String
    private var mPassword: String? = null
    private var mId: Int = -1

    companion object {
        @JvmStatic
        fun openActivity(context: Context, fromActivity: FromActivityEnum, phoneNumber: String, id: Int, password: String? = null) {
            val intent = Intent(context, CaptchaActivity::class.java)
            intent.putExtra("fromActivity", fromActivity)
            intent.putExtra("phoneNumberWithCc", phoneNumber)
            if (password != null) {
                intent.putExtra("password", password)
            }
            intent.putExtra("id", id)
            context.startActivity(intent)
        }
    }

    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_captcha)
        mFromActivity = intent.getSerializableExtra("fromActivity") as FromActivityEnum
        mPhoneNumber = intent.getStringExtra("phoneNumberWithCc")
        mPassword = intent.getStringExtra("password")
        mId = intent.getIntExtra("id", -1)
        mBinding.vm = ViewModel()
    }

    override fun createHandler() {
        mBinding.nbv.setOnBackListener { onBackPressed() }
        mBinding.cetCaptcha.startFilter(FilterType.CAPTCHA)
        mBinding.tvCaptcha.setLeftTime(CountDownTimerWrapper.countDown.toInt())
    }

    override fun loadData(savedInstanceState: Bundle?) {

    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding.tvCaptcha.stopCount()
    }

    inner class ViewModel {
        fun onAccountActivated() {

            val code = mBinding.cetCaptcha.text.toString().trim()
            if (IsEmpty.string(code)) {
                ToastUtil.showShort("请填写验证码")
            } else {
                val pd = DialogUtil.showIndeterminate(mContext)
                when (mFromActivity) {
                    FromActivityEnum.ForgotPasswordActivity -> {
                        pd.dismiss()

                        


                    }
                    FromActivityEnum.RegisterActivity -> {
                        val registerReq = RegisterReq()
                        registerReq.password = mPassword
                        registerReq.verification_code = code
                        registerReq.id = mId
                        HttpMethods.getLoginService()
                                .register(registerReq)
                                .compose(RxUtil.applyScheduler())
                                .compose(bindToLifecycle())
                                .subscribe(object : HttpSubscriber<Profile>() {
                                    override fun onSuccess(response: Profile?) {
                                        pd.dismiss()
                                        ToastUtil.showShort("注册完成")
                                        
                                        ProfileUtil.save(response)
                                        CompleteProfileActivity.openActivity(mContext)
                                        finish()
                                    }

                                    override fun onFailure(errMsg: String?, response: Profile?, code: Int) {
                                        pd.dismiss()
                                        DialogUtil.showError(mContext, errMsg)
                                    }
                                })
                    }
                    FromActivityEnum.ThirdPartyActivity -> {
                        val finishBindMobileReq = FinishBindMobileReq(mPhoneNumber, code)
                        HttpMethods.getLoginService()
                                .finishBindMobile(finishBindMobileReq)
                                .compose(RxUtil.applyScheduler())
                                .compose(bindToLifecycle())
                                .subscribe(object : HttpSubscriber<Profile>() {
                                    override fun onSuccess(response: Profile?) {
                                        pd.dismiss()
                                        ToastUtil.showShort("注册完成")
                                        
                                        ProfileUtil.save(response)
                                        CompleteProfileActivity.openActivity(mContext)
                                        finish()

                                    }

                                    override fun onFailure(errMsg: String?, response: Profile?, code: Int) {
                                        pd.dismiss()
                                        DialogUtil.showError(mContext, errMsg)
                                    }
                                })

                    }
                }


            }

        }

        fun onCaptcha() {
            mBinding.tvCaptcha.startCount()

            when (mFromActivity) {
                FromActivityEnum.ForgotPasswordActivity -> {
















                }
                FromActivityEnum.RegisterActivity -> {
                    val sendCode = SendRegisterCaptchaReq()
                    sendCode.mobile = mPhoneNumber

                    HttpMethods.getLoginService()
                            .sendRegisterCaptcha(sendCode)
                            .compose(RxUtil.applyScheduler())
                            .compose(bindToLifecycle())
                            .subscribe(object : HttpSubscriber<SendRegisterCaptchaRes>() {
                                override fun onSuccess(response: SendRegisterCaptchaRes?) {
                                    ToastUtil.showShort("验证码已发送，请查收短信")
                                }

                                override fun onFailure(errMsg: String?, response: SendRegisterCaptchaRes?, code: Int) {
                                    DialogUtil.showError(mContext, errMsg)
                                    mBinding.tvCaptcha.stopCount()
                                }
                            })

                }
                FromActivityEnum.ThirdPartyActivity -> {

                    val bindMobileReq = BindMobileReq()
                    bindMobileReq.mobile = mPhoneNumber


                    HttpMethods.getLoginService()
                            .bindMobile(bindMobileReq)
                            .compose(RxUtil.applyNullScheduler())
                            .subscribe(object : NullSubscriber() {
                                override fun onSuccess() {
                                    ToastUtil.showShort("验证码已发送，请查收短信")
                                }

                                override fun onFailure(errMsg: String?, code: Int) {
                                    DialogUtil.showError(mContext, errMsg)
                                    mBinding.tvCaptcha.stopCount()
                                }
                            })


                }
            }

        }
    }
}

enum class FromActivityEnum {
    ForgotPasswordActivity,
    RegisterActivity,
    ThirdPartyActivity
}